home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xascii / xascii.horiz.c < prev    next >
C/C++ Source or Header  |  1995-06-22  |  19KB  |  523 lines

  1. /*****************************************************************************
  2. * XAscii                                                      by Ken Kirksey *
  3. *                                                                            *
  4. * XAscii displays a table of ascii characters and their respective values.   *
  5. * The user choses which format the values are displayed in:  either hex,     *
  6. * decimal or octal.                                                          *
  7. *                                                                            *
  8. *
  9. * BASICALLY THE WAY IT WORKS                                                 *
  10. *    In xascii.h four static arrays of strings are declared, one for      *
  11. *      ascii characters, one for decimal values, one for hex values and one *
  12. *    for octal values.  Each of these are broken up into five separate    *
  13. *    strings, each of which make up a column (label widget) in the xascii *
  14. *    window.  The ascii value columns remain static, they never change.   *
  15. *    The label widgets that hold the values, however, change whenever the *
  16. *    user changes the notational mode (clicks on the dec ,hex, or octal   *
  17. *       button).  What happens when one of these buttons is clicked is that  *
  18. *       an XtVaSetValues is done on the value label widgets to change their  *
  19. *    label resource to display the values in the notation specifiedby the *
  20. *    button press.                                                        *
  21. *                                                                            *
  22. * Send all kudos, complaints, suggestions, or bug reports to:                *
  23. *        Ken Kirksey  (kkirksey@eng.auburn.edu)                       *
  24. *                                                                            *
  25. * BTW:  I use 4 column tabs for coding, so if your text editor isn't set for *
  26. *         4 column tabs, this program probably looks really funny.     *
  27. *                                                                       *
  28. ******************************************************************************
  29. * Version 1.1                                                       5 Aug 91 *
  30. *                                                                            *
  31. *  Removed the #include <strings.h> as this was a portability no-no.         *
  32. *  Added an Imakefile (courtesy of Dave Elliot) to the release archive.      *
  33. *                                                               *
  34. ******************************************************************************
  35. * Version 1.2                                                       6 Aug 91 *
  36. *                                                                            *
  37. * Fixed the problem that was causing my use of the valueFont resource to     *
  38. * kill the program.  The bug was pointed out by Dave Brooks.                 *
  39. *
  40. *
  41. * Modded 19.08.93 Hops
  42. * change layout                                          *
  43. *****************************************************************************/
  44.                                                                            
  45.  
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <X11/Intrinsic.h>
  49. #include <X11/Shell.h>
  50. #include <X11/Xos.h>
  51. #include <X11/StringDefs.h>
  52. #include <X11/Xaw/Command.h>
  53. #include <X11/Xaw/Toggle.h>
  54. #include <X11/Xaw/Label.h>    
  55. #include <X11/Xaw/Form.h>
  56. #include "xascii.h"
  57.  
  58. #define COLS    16
  59. #define ROWS    8
  60.  
  61. /*===========================================================================+
  62. | Function Declarations                                                      |
  63. +===========================================================================*/
  64. static void Quit(),    /* Callbacks                            */
  65.     ToHex(),
  66.     ToDec(),
  67.     ToOct(),
  68.     Done(),
  69.     AboutXascii(),   /* Actions                */
  70.     Syntax();        /* Utilitie                */
  71. static char *ascii_val();
  72.  
  73. /*===========================================================================+
  74. | Global Variable Declarations                                               |
  75. +===========================================================================*/
  76.  
  77. static XtAppContext appl_context;
  78.  
  79. static XtActionsRec about_box[]= {
  80.         {"about_xascii", AboutXascii},
  81. };
  82.  
  83. static Widget     toplevel,
  84.                 base_win,
  85.                 title_label,
  86.                 quit_button,
  87.                 toggles[3],
  88.                 ascii_list[COLS],
  89.                 nm_list[COLS],
  90.                 value_list[COLS],
  91.                 pshell,
  92.                 about_base_win,
  93.                 about_label,
  94.                 done_button;
  95.  
  96. char        **hex_columns,
  97.             **decimal_columns,
  98.             **octal_columns,
  99.             **ascii_columns,
  100.             **nm_columns;
  101.  
  102. /*===========================================================================+
  103. | MAIN                                                                       | +===========================================================================*/
  104. main (argc,argv)
  105.     int     argc; 
  106.     char     ** argv;
  107. {
  108.     int        i, j;
  109.     char buf[100], buf1[10];
  110.     
  111. /*---------------------------------------------------------------------------+
  112. | Break up the Ascii table and values into separate columns (8 rows, 16 cols)|
  113. | for display.                                                               |
  114. +---------------------------------------------------------------------------*/
  115.     ascii_columns   = (char **)XtCalloc (COLS, sizeof (char *));
  116.     nm_columns      = (char **)XtCalloc (COLS, sizeof (char *));
  117.     hex_columns     = (char **) XtCalloc (COLS, sizeof (char *));
  118.     decimal_columns = (char **) XtCalloc (COLS, sizeof (char *));
  119.     octal_columns   = (char **) XtCalloc (COLS, sizeof (char *));
  120.  
  121.     for (i=0; i<ROWS; i++)
  122.     {
  123.         nm_columns[i]    = (char *)  XtCalloc (90, sizeof (char));
  124.         ascii_columns[i]    = (char *)  XtCalloc (90, sizeof (char));
  125.         hex_columns[i]      = (char *)  XtCalloc (90, sizeof (char));
  126.         decimal_columns[i]  = (char *)  XtCalloc (90, sizeof (char));
  127.         octal_columns[i]    = (char *)  XtCalloc (90, sizeof (char));
  128.     
  129.      for (j= (i*COLS); j < ((i+1) * COLS); j++) 
  130.         {
  131.             sprintf( buf, "%3.3s  ", ascii_val(j, buf1) );
  132.             strcat (ascii_columns[i], buf);
  133.  
  134.  
  135.             sprintf( buf, "%3.3s  ", name_values[j]);
  136.             strcat (nm_columns[i], buf );
  137.  
  138.             sprintf(buf, "%3x  ", j ) ;
  139.             strcat (hex_columns[i], buf);
  140.  
  141.             sprintf(buf, "%3d  ", j ) ;
  142.             strcat (decimal_columns[i], buf);
  143.  
  144.             sprintf(buf, " %03o ", j ) ;
  145.             strcat (octal_columns[i], buf);
  146.         }
  147.     }
  148.  
  149.     /*-----------------------------------------------------------------------+
  150.     | Begin Widget Initialization.                                           |
  151.     +-----------------------------------------------------------------------*/     
  152.     toplevel = XtAppInitialize(
  153.                     &appl_context,
  154.                     "XAscii",
  155.                     options,
  156.                     XtNumber(options),
  157.                     &argc,argv,( String *) fallbackres /*NULL*/, NULL,0);
  158.  
  159.     /*-----------------------------------------------------------------------+
  160.     | Check for Invalid command line options                                 |
  161.     +-----------------------------------------------------------------------*/
  162.     if (argc > 1)
  163.         Syntax (argc, argv);
  164.  
  165.     XtVaGetApplicationResources (toplevel,
  166.                     &app_data,
  167.                     resources,
  168.                     XtNumber (resources),
  169.                     NULL);
  170.  
  171.     base_win = XtVaCreateManagedWidget(
  172.                     "base_win", 
  173.                     formWidgetClass, 
  174.                     toplevel,
  175.                     NULL, 0);
  176.  
  177.     /*----------------------------------------------------------------------+
  178.     |  Note the translation that invokes that about box pop up on a button  |
  179.     |  click in this label widget.                                          |
  180.     +----------------------------------------------------------------------*/
  181.     title_label    = XtVaCreateManagedWidget (
  182.                     "title_label",
  183.                     labelWidgetClass,
  184.                     base_win,
  185.                         XtNforeground, app_data.highlight_color,
  186.                         XtNlabel, "X ASCII Chart",
  187.                         XtNwidth, 408,
  188.                         XtNtranslations, XtParseTranslationTable             
  189.                             (defaultTranslations),
  190.                         XtVaTypedArg,
  191.                             XtNbackground,
  192.                             XtRString,
  193.                             white_bg,
  194.                             sizeof (white_bg),
  195.                         XtVaTypedArg,
  196.                             XtNfont,
  197.                             XtRString,
  198.                             title_font,
  199.                             sizeof (title_font),
  200.                     NULL, 0);
  201.  
  202.     /*-----------------------------------------------------------------------+
  203.     |Create the label widgets for chart display. Default value mode is dec.  |
  204.     +-----------------------------------------------------------------------*/
  205.     for (i=0; i<ROWS;  i++)
  206.     {
  207.  
  208.         ascii_list[i] = XtVaCreateManagedWidget(
  209.                         "ascii_list",
  210.                         labelWidgetClass,
  211.                         base_win,
  212.                             XtNlabel, ascii_columns[i],
  213.                             XtNfromVert, title_label,
  214.  /*
  215.                             XtNfont, app_data.value_font,  
  216.  */                       NULL);
  217.  
  218.  
  219.         value_list[i] = XtVaCreateManagedWidget(
  220.                         "value_list",
  221.                         labelWidgetClass,
  222.                         base_win,
  223.                             XtNlabel, decimal_columns[i],
  224.                             XtNfromVert, ascii_list[i], 
  225.                             XtNforeground, app_data.highlight_color,
  226. /*
  227.                             XtNfont, app_data.value_font,  
  228.  */
  229.  
  230.                         NULL);
  231.  
  232.         nm_list[i] = XtVaCreateManagedWidget(
  233.                         "name_list",
  234.                         labelWidgetClass,
  235.                         base_win,
  236.                             XtNfromVert, value_list[i], 
  237.                             XtNlabel, nm_columns[i],
  238. /*
  239.                             XtNfont, app_data.value_font,  
  240. */
  241.                         NULL);
  242.  
  243.  
  244.  
  245.     }
  246.  
  247.     /*  ascii, value, name */
  248.     for (i=1; i<ROWS; i++)
  249.     {
  250.         XtVaSetValues (ascii_list[i], XtNfromVert, nm_list[i-1], NULL);
  251.     }
  252.  
  253.     /*-----------------------------------------------------------------------+
  254.     | Create the buttons for value mode selection.  Make them a radio group, |
  255.     | so that only one will be selected (highlighted) at a time.             |
  256.     +-----------------------------------------------------------------------*/
  257.     for (i=0; i<3; i++)
  258.     {
  259.     char name[40];
  260.  
  261.         sprintf(name, "toggle%d", i );
  262.         
  263.         toggles[i] = XtVaCreateManagedWidget (
  264.                     name,
  265.                     toggleWidgetClass, 
  266.                     base_win,
  267.                         XtNforeground, app_data.highlight_color,
  268.                         XtNborderColor, app_data.highlight_color,
  269.                         XtNfromVert, nm_list[ROWS-1],
  270.                         XtNwidth, 95,
  271.                         XtNresize, FALSE,
  272.                     NULL, 0);
  273.  
  274.         if (i != 0)
  275.             XtVaSetValues(toggles[i], XtNfromHoriz, toggles[i-1], NULL);
  276.  
  277.         XtVaSetValues (toggles[i], XtNradioGroup, toggles[0], NULL);
  278.     }
  279.  
  280.     XtVaSetValues (toggles[0],
  281.                     XtNlabel,    "Hex",
  282.                     NULL);
  283.  
  284.     XtVaSetValues (toggles[1],
  285.                     XtNstate, TRUE,
  286.                     XtNlabel, "Decimal", 
  287.                     NULL);
  288.  
  289.     XtVaSetValues (toggles[2],
  290.                     XtNlabel,    "Octal",
  291.                     NULL);
  292.  
  293.     XtAddCallback (toggles[0], XtNcallback, ToHex, NULL); 
  294.     XtAddCallback (toggles[1], XtNcallback, ToDec, NULL);
  295.      XtAddCallback (toggles[2], XtNcallback, ToOct, NULL); 
  296.  
  297.     
  298.     quit_button = XtVaCreateManagedWidget (
  299.                     "quit_button", 
  300.                     commandWidgetClass, 
  301.                     base_win,
  302.                     XtNlabel, "Quit",
  303.                     XtNwidth, 100,
  304.                     NULL, 0);
  305.  
  306.     XtAddCallback (quit_button, XtNcallback, Quit, NULL);
  307.  
  308.     /*-----------------------------------------------------------------------+
  309.     | Create the About Box.  Invoked on click in title label.                |
  310.     +-----------------------------------------------------------------------*/
  311.     pshell = XtVaCreatePopupShell (
  312.                     "pshell",
  313.                     transientShellWidgetClass,
  314.                     toplevel,
  315.                     NULL);
  316.  
  317.     about_base_win = XtVaCreateManagedWidget(
  318.                     "about_base_win",
  319.                     formWidgetClass,
  320.                     pshell,
  321.  
  322.                     NULL);
  323.  
  324.     about_label = XtVaCreateManagedWidget(
  325.                     "about_label",
  326.                     labelWidgetClass,
  327.                     about_base_win,
  328.                         XtVaTypedArg,
  329.                             XtNbackground,
  330.                             XtRString,
  331.                             white_bg,
  332.                             sizeof (white_bg),
  333.                         XtNlabel, about_text,
  334.                         XtNforeground, app_data.highlight_color,
  335.                         XtNborderColor, app_data.highlight_color,
  336.                         XtNborderWidth, 4,
  337.                     NULL);
  338.  
  339.     done_button = XtVaCreateManagedWidget(
  340.                     "done_button",
  341.                     commandWidgetClass,
  342.                     about_base_win,
  343.                         XtNfromVert, about_label,
  344. /*
  345.                         XtVaTypedArg,
  346.                             XtNbackground,
  347.                             XtRString,
  348.                             white_bg,
  349.                             sizeof (white_bg),
  350.                         XtNvertDistance, 10,
  351.                         XtNhorizDistance, 165,
  352.                         XtNlabel, "Close",
  353. */                            
  354.                     NULL);
  355.  
  356.     XtAddCallback (done_button, XtNcallback, Done, NULL);
  357.  
  358.     XtAppAddActions (appl_context, about_box, XtNumber(about_box) );
  359.  
  360.      XtRealizeWidget(toplevel);
  361.      XtAppMainLoop(appl_context);
  362. }
  363.  
  364. /*===========================================================================+
  365. | CALLBACK FUNCTIONS                                                          | +===========================================================================*/
  366.  
  367. /*---------------------------------------------------------------------------+
  368. | Quit                                                                       |
  369. |         Quit the application.  Invoked by click on quit button.              |
  370. +---------------------------------------------------------------------------*/     static void Quit (w,ignore1,ignore2)
  371. Widget w; XtPointer ignore1, ignore2;
  372. {   XtDestroyApplicationContext(appl_context);
  373.     exit(0);
  374. }
  375.  
  376. /*---------------------------------------------------------------------------+
  377. | ToHex                                                                      |
  378. |         Changes the ascii character values to Hex notation.                  |
  379. +---------------------------------------------------------------------------*/  
  380. static void ToHex (w,ignore1,ignore2)
  381. Widget w; XtPointer ignore1, ignore2;
  382.   int i;
  383.  
  384.     for (i=0; i<ROWS; i++)
  385.         XtVaSetValues (value_list[i], XtNlabel, hex_columns[i], NULL);
  386.  
  387. }
  388.  
  389.  
  390. /*---------------------------------------------------------------------------+
  391. | ToDec                                                                      |
  392. |         Changes the ascii character values to Decimal notation.              |
  393. +---------------------------------------------------------------------------*/  
  394. static void ToDec (w,ignore1,ignore2)
  395. Widget w; XtPointer ignore1, ignore2;
  396.   int i;
  397.  
  398.     for (i=0; i<ROWS; i++)
  399.         XtVaSetValues (value_list[i], XtNlabel, decimal_columns[i], NULL);
  400.  
  401. }
  402.  
  403.  
  404. /*---------------------------------------------------------------------------+
  405. | ToOct                                                                      |
  406. |         Changest the ascii character values to Octal notation.               |
  407. +---------------------------------------------------------------------------*/  
  408. static void ToOct (w,ignore1,ignore2)
  409. Widget w; XtPointer ignore1, ignore2;
  410.   int i;
  411.  
  412.     for (i=0; i<ROWS; i++)
  413.         XtVaSetValues (value_list[i], XtNlabel, octal_columns[i], NULL);
  414.  
  415. }
  416.  
  417.  
  418. /*---------------------------------------------------------------------------+
  419. | Done                                                                       |
  420. |         Pops down the about box popup shell.                                 |
  421. +---------------------------------------------------------------------------*/  
  422. static void Done (w,ignore1,ignore2)
  423. Widget w; XtPointer ignore1, ignore2;
  424.   int i;
  425.  
  426.     XtPopdown (pshell);
  427. }
  428.  
  429.  
  430. /*===========================================================================+
  431. | ACTION FUNCTIONS                                                               | +===========================================================================*/
  432.  
  433. /*---------------------------------------------------------------------------+
  434. | AboutXascii                                                                |
  435. |             Positions and pops up the about box popup shell.                 |
  436. +---------------------------------------------------------------------------*/  
  437. static void AboutXascii (w, event, nuffin1, nuffin2)
  438. Widget            w;
  439. XButtonEvent    event;
  440. String            *nuffin1;
  441. Cardinal        *nuffin2;
  442. {    
  443.     Position    x, 
  444.                 y;
  445.  
  446.     Dimension    width, 
  447.                 height;
  448.  
  449.     int            i;
  450.  
  451.     XtVaGetValues (toplevel,
  452.             XtNwidth, &width,
  453.             XtNheight, &height,
  454.             NULL);
  455.  
  456.     XtTranslateCoords (toplevel,
  457.             (Position) 0,
  458.             (Position) height/2,
  459.             &x, &y,
  460.             NULL);
  461.  
  462.     XtVaSetValues (pshell,
  463.             XtNx, x,
  464.             XtNy, y,
  465.             NULL);
  466.  
  467.     XtPopup (pshell, XtGrabNonexclusive);
  468. }
  469.  
  470. /*===========================================================================+
  471. | UTILITY FUNCTIONS                                                              | +===========================================================================*/
  472.  
  473. /*---------------------------------------------------------------------------+
  474. | Syntax                                                                     |
  475. |        Parses off bad command line options (i.e. the stuff left over when   |
  476. |         XtVaAppInitialize is through).                                     |
  477. +---------------------------------------------------------------------------*/       static void Syntax (argc, argv)
  478. int     argc;
  479. char    **argv;
  480. {
  481.     int     i,
  482.             err = 0;
  483.  
  484.     for (i=1; i < argc; i++)
  485.     {
  486.         if (!err++)
  487.             fprintf (stderr, "\nxascii: unknown command line option\n");
  488.  
  489.         fprintf (stderr, "option:  %s\n", argv[i]);
  490.     }
  491.  
  492. }
  493.  
  494.         
  495. static char *ascii_val(j, s )
  496. int j;
  497. char *s;
  498. {
  499.     if ( j < ' ' )
  500.         sprintf(s, "^%c", j | 0x40 );
  501.     else if ( j == 127 )
  502.         sprintf(s, "^?");
  503.     else
  504.         sprintf(s, "%c", j );
  505.  
  506.     return s;        
  507. }
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.